package by.gravity.web.services;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import by.gravity.provider.GrsuGsonSheduleProvider;
import by.gravity.provider.GsonSheduleProvider;
import by.gravity.web.constants.SheduleConstants;
import by.gravity.web.util.StringUtil;

public class CommonServlet extends HttpServlet implements SheduleConstants {

	private static final long serialVersionUID = 2947144683333303510L;

	private GsonSheduleProvider provider;

	public CommonServlet() {
		provider = new GrsuGsonSheduleProvider();
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/plain; charset=UTF-8");
	}

	public GsonSheduleProvider getProvider() {
		return provider;
	}

	protected void printExeption(PrintWriter writer, Throwable throwable) {
		writer.append(throwable.getMessage());
	}

	protected String getSpecialityName(HttpServletRequest request) {
		return request.getParameter(SPECIALITY);
	}

	protected String getGroupName(HttpServletRequest request) {
		return request.getParameter(GROUP);
	}

	protected String getKursName(HttpServletRequest request) {
		return request.getParameter(KURS);
	}
	
	protected Long getStartWeekDate(HttpServletRequest request){
		String startDate = request.getParameter(WEEK_START_DATE);
		if(!StringUtil.isEmpty(startDate)){
			return Long.parseLong(startDate);
		}
		
		return null;
	}
}
